//
//  MyInfoViewController.m
//  parent
//
//  Created by yanzhen on 15/7/17.
//  Copyright (c) 2015年 ci123. All rights reserved.
//

#import "MyInfoViewController.h"
#import "EditInfoViewController.h"
#import "MytopicViewController.h"
#import "MailViewController.h"
#import "MyorderViewController.h"
#import "FavoriteViewController.h"
#import "SettingViewController.h"
#import "AboutViewController.h"
#import "GoldViewController.h"
#import "MyorderdetailViewController.h"
#import "Config.h"
#import "MyloginViewController.h"
#import "UIImageView+WebCache.h"

@interface MyInfoViewController ()<UIActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UIView *goldView;
@property (weak, nonatomic) IBOutlet UIView *orderView;
@property (weak, nonatomic) IBOutlet UIView *favView;
@property (weak, nonatomic) IBOutlet UIView *settingView;
@property (weak, nonatomic) IBOutlet UIView *aboutusView;
@property (weak, nonatomic) IBOutlet UIView *contactView;
@property (weak, nonatomic) IBOutlet UIImageView *photo;
@property (weak, nonatomic) IBOutlet UILabel *nickname;
@property (weak, nonatomic) IBOutlet UILabel *No;
@property (weak, nonatomic) IBOutlet UILabel *goldNum;
@property (weak, nonatomic) IBOutlet UILabel *totalGold;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *containerViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nicknameWidth;

@end

@implementation MyInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"我的";
    // Do any additional setup after loading the view from its nib.
    _goldView.userInteractionEnabled = YES;
    UITapGestureRecognizer *reco = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gold:)];
    [_goldView addGestureRecognizer:reco];
    
    _orderView.userInteractionEnabled = YES;
    UITapGestureRecognizer *reco1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(order:)];
    [_orderView addGestureRecognizer:reco1];
    
    _settingView.userInteractionEnabled = YES;
    UITapGestureRecognizer *reco2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(setting:)];
    [_settingView addGestureRecognizer:reco2];
    
    _aboutusView.userInteractionEnabled = YES;
    UITapGestureRecognizer *reco3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(aboutus:)];
    [_aboutusView addGestureRecognizer:reco3];
    
    _favView.userInteractionEnabled = YES;
    UITapGestureRecognizer *reco4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(fav:)];
    [_favView addGestureRecognizer:reco4];
    
    _contactView.userInteractionEnabled = YES;
    UITapGestureRecognizer *reco5 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(contact:)];
    [_contactView addGestureRecognizer:reco5];
    _containerViewHeight.constant = 554;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resetInfo:) name:kReloadInfo object:nil];
    [self resetInfo:nil];
}
-(void)resetInfo:(id)sender
{
    NSDictionary *myInfo = [[Config Instance]getUInfo];
    if([Config Instance].isCookie&&myInfo){
        NSString *pic = [myInfo objectForKey:@"pic"];
        if([[myInfo objectForKey:@"pic"] isEqual:[NSNull null]]){
        
        }else{
            [_photo sd_setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"default_avatar"]];
        }
        _nickname.text = [myInfo objectForKey:@"nickname"];
        
    }else{
        _nickname.text = @"请登录";
        _photo.image = [UIImage imageNamed:@"default_avatar"];
    }
    
}

-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    CGSize size = CGSizeMake(100,30);
    NSString *nickname = _nickname.text;
    UIFont *font = [UIFont systemFontOfSize:18.0];
    CGSize labelsize = [nickname sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
    _nicknameWidth.constant = labelsize.width;
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    _containerViewHeight.constant = 554;
}
-(void)contact:(id)sender
{
        UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                      initWithTitle:nil
                                      delegate:self
                                      cancelButtonTitle:@"取消"
                                      destructiveButtonTitle:@"400-012-8815"
                                      otherButtonTitles:nil];
        actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
        [actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex ==0){
        UIApplication *myApp = [UIApplication sharedApplication];
        NSString *theCall = [NSString stringWithFormat:@"tel://4000128815"];
        [myApp openURL:[NSURL URLWithString:theCall]];
    }
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}
-(void)fav:(id)sender
{
    if([[Config Instance]isCookie]){
        FavoriteViewController *fav = [FavoriteViewController new];
        fav.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:fav animated:YES];
    }else{
       MyloginViewController *login = [MyloginViewController shareController];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:login];
        [self presentViewController:nav animated:YES completion:^{
            
        }];

    }
}
-(void)aboutus:(id)sender
{
    AboutViewController *about = [[AboutViewController alloc]init];
    about.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:about animated:YES];
}
-(void)setting:(id)sender
{
    if([[Config Instance]isCookie]){
        SettingViewController *sett = [[SettingViewController alloc] init];
        sett.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:sett animated:YES];
    }else{
       MyloginViewController *login = [MyloginViewController shareController];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:login];
        [self presentViewController:nav animated:YES completion:^{
            
        }];
        
    }
}
-(void)order:(id)sender
{
    if([[Config Instance]isCookie]){
        MyorderViewController *order = [[MyorderViewController alloc]init];
        order.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:order animated:YES];
    }else{
        MyloginViewController *login = [MyloginViewController shareController];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:login];
        [self presentViewController:nav animated:YES completion:^{
            
        }];
    }
}
-(void)gold:(id)sender
{
    if([[Config Instance]isCookie]){
        GoldViewController *edit = [[GoldViewController alloc]init];
        edit.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:edit animated:YES];
    }else{
        MyloginViewController *login = [MyloginViewController shareController];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:login];
        [self presentViewController:nav animated:YES completion:^{
            
        }];
    }
}

- (IBAction)editInfo:(id)sender {
    if([[Config Instance]isCookie]){
        EditInfoViewController *edit = [[EditInfoViewController alloc]init];
        edit.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:edit animated:YES];
    }else{
       MyloginViewController *login = [MyloginViewController shareController];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:login];
        [self presentViewController:nav animated:YES completion:^{
            
        }];

    }
    
}
- (IBAction)myTopic:(id)sender {
    if([[Config Instance]isCookie]){
    MytopicViewController *edit = [[MytopicViewController alloc]init];
    edit.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:edit animated:YES];
    }else{
        MyloginViewController *login = [MyloginViewController shareController];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:login];
        [self presentViewController:nav animated:YES completion:^{
            
        }];
        
    }
}
- (IBAction)myEmail:(id)sender {
    if([[Config Instance]isCookie]){
        MailViewController *edit = [[MailViewController alloc]init];
        edit.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:edit animated:YES];
    }else{
       MyloginViewController *login = [MyloginViewController shareController];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:login];
        [self presentViewController:nav animated:YES completion:^{
            
        }];

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
